global Zbase = "lag_l lag_m lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_lm lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km"
global INS =  "dm_lag_q_m dm_lag_l dm_lag_m dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D  dm_lag_I_sq_lm dm_lag_I_sq_micro_inv_km dm_lag_I_sq_micro_inv_D_km"
local spec = "new_klm"

clear all
use "$working/prowess_wits_estimation_qonly_klm.dta", clear
		
xtset product_id year
			
foreach v in q_m k l m I_sq ln_shares ln_IND ln_IVS D_ln_micro_inv D_micro_inv D_ln_inv_base D_inv_base k_alt I_sq_micro_inv I_sq_micro_inv_D I_sq_inv_base I_sq_inv_base_D  ///
	I_sq_km I_sq_lm  I_sq_micro_inv_km I_sq_micro_inv_D_km I_sq_inv_base_km I_sq_inv_base_D_km ln_wIPT_IND_CHN {
		gen lag_`v' = L.`v'
}
			
foreach v in q_m k l m I_sq I_sq_km I_sq_lm ln_shares ln_IND ln_IVS ln_wIPT_IND_CHN  {
	drop if lag_`v'==. | `v'==.
}
			
foreach v in  $Zbase {
	drop if `v'==.
}
			
bysort year: gen total_year_obs = _N
drop if total_year_obs==1
			
quietly tabulate year, generate(year_fe)
			
generate everyone = 1
			
unique year, by(everyone) generate(total_years)
sum total_years
local count_year_fe = r(mean)
			
			
foreach v in q_m lag_q_m k lag_k l lag_l m lag_m I_sq lag_I_sq ln_shares lag_ln_shares ln_IND ln_IVS lag_ln_IND lag_ln_IVS ln_wIPT_IND_CHN lag_ln_wIPT_IND_CHN ///
 I_sq_km I_sq_lm  lag_I_sq_km lag_I_sq_lm  {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
foreach v in $ZnonX_base {
	egen mean_`v' = mean(`v'), by(up12)
	gen dm_`v' = `v' - mean_`v'
}
			
			
forvalues i = 1(1)`count_year_fe' {
	egen mean_year_fe`i' = mean(year_fe`i'), by(up12)
	gen dm_year_fe`i' = year_fe`i' - mean_year_fe`i'
}

preserve
	bysort co_code1 year: generate count_thing = _n
	keep if count_thing==1
	keep co_code1 year
	save "$working/ProdQ_`spec'_sampleFS", replace
restore			


preserve
	keep co_code1 year product_name5
	isid co_code1 year product_name5
	save "$working/ProdQ_`spec'_sampleFS_prod", replace
restore

			
 gmm (dm_q_m - {rho=0.9}*dm_lag_q_m - {beta_l=0.1}*(dm_l + dm_ln_shares) + {rho}*{beta_l}*(dm_lag_l + dm_lag_ln_shares) ///
	- {beta_k=0.1}*(dm_k+ dm_ln_shares) + {rho}*{beta_k}*(dm_lag_k+ dm_lag_ln_shares) ///
	- {beta_m=0.8}*(dm_m+ dm_ln_shares) + {rho}*{beta_m}*(dm_lag_m+ dm_lag_ln_shares) ///
	- {beta_lk=0}*dm_I_sq + {rho}*{beta_lk}*dm_lag_I_sq ///
	- {beta_km=0}*dm_I_sq_km + {rho}*{beta_km}*dm_lag_I_sq_km ///
	- {beta_lm=0}*dm_I_sq_lm + {rho}*{beta_lm}*dm_lag_I_sq_lm ///
	-{xb: dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN}), ///
	instruments($INS dm_year_fe* dm_lag_ln_IND dm_lag_ln_wIPT_IND_CHN, nocons) onestep quickderivatives technique(bfgs) vce(cluster co_code)
		
gen rho_hat = [rho]_cons
gen rho_hat_se = _se[rho:_cons]
		
gen beta_k_hat_q = [beta_k]_cons
gen beta_l_hat_q = [beta_l]_cons
gen beta_m_hat_q = [beta_m]_cons
		
gen beta_lk_hat_q = [beta_lk]_cons
gen beta_km_hat_q = [beta_km]_cons
gen beta_lm_hat_q = [beta_lm]_cons
		
gen beta_k_hat_q_se = _se[beta_k:_cons]
gen beta_l_hat_q_se = _se[beta_l:_cons]
gen beta_m_hat_q_se = _se[beta_m:_cons]
		
gen beta_lk_hat_q_se = _se[beta_lk:_cons]
gen beta_km_hat_q_se = _se[beta_km:_cons]
gen beta_lm_hat_q_se = _se[beta_lm:_cons]
		
gen beta_lag_ln_IND = _b[xb:dm_lag_ln_IND]
gen beta_lag_ln_IND_se = _se[xb: dm_lag_ln_IND]

gen beta_lag_MA = _b[xb: dm_lag_ln_wIPT_IND_CHN]
gen beta_lag_MA_se = _se[xb: dm_lag_ln_wIPT_IND_CHN]
		
generate rts = beta_k_hat_q + beta_l_hat_q + beta_m_hat_q
		
lincom [beta_k]_cons+[beta_l]_cons +[beta_m]_cons
generate rts_se = r(se)
		
gen theta_k_hat_q= beta_k_hat_q + beta_lk_hat_q*(l - k) + beta_km_hat_q*(m - k)
gen theta_l_hat_q = beta_l_hat_q + beta_lk_hat_q*(k - l) + beta_lm_hat_q*(m - l)
gen theta_m_hat_q = beta_m_hat_q + beta_km_hat_q*(k - m) + beta_lm_hat_q*(l - m)

	foreach ind in 10 13 17 20 21 22 23 24 25 26 27 28 29  {
		preserve
			keep if nic_08_2dig == "`ind'"	
			
			foreach variable in k_hat_q l_hat_q m_hat_q {
				sum theta_`variable', detail
				generate theta_`variable'_mean = r(mean)
				generate theta_`variable'_med = r(p50)
				generate theta_`variable'_sd = r(sd)
			}
			
			contract nic_08_2dig rho_hat rho_hat_se beta_k_hat_q beta_l_hat_q  beta_m_hat_q /// 
			beta_lk_hat_q beta_km_hat_q beta_lm_hat_q beta_lag_ln_IND beta_lag_ln_IND_se ///
			theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med theta_m_hat_q_mean theta_m_hat_q_med ///
			theta_k_hat_q_sd theta_l_hat_q_sd theta_m_hat_q_sd rts rts_se
		
			save "$working/Q_klm_opf_`ind'", replace
		restore
	}

		
foreach variable in k_hat_q l_hat_q m_hat_q {
	sum theta_`variable', detail
	generate theta_`variable'_mean = r(mean)
	generate theta_`variable'_med = r(p50)
	generate theta_`variable'_sd = r(sd)
}
		
contract nic_08_2dig rho_hat rho_hat_se beta_k_hat_q beta_l_hat_q  beta_m_hat_q /// 
	beta_lk_hat_q beta_km_hat_q beta_lm_hat_q beta_lag_ln_IND beta_lag_ln_IND_se ///
	theta_k_hat_q_mean theta_k_hat_q_med theta_l_hat_q_mean theta_l_hat_q_med theta_m_hat_q_mean theta_m_hat_q_med ///
	theta_k_hat_q_sd theta_l_hat_q_sd theta_m_hat_q_sd rts rts_se beta_lag_MA beta_lag_MA_se ///
	beta_k_hat_q_se beta_l_hat_q_se beta_m_hat_q_se /// 
	beta_lk_hat_q_se beta_km_hat_q_se beta_lm_hat_q_se
		
	rename _freq Observations

	save "$working/ProdQ_`spec'", replace
	
collapse (mean) beta_k_hat_q beta_l_hat_q  beta_m_hat_q /// 
	beta_lk_hat_q beta_km_hat_q beta_lm_hat_q  (sum) Observations

generate bs = 0

save "$BS_fold/bs_ProdQ_`spec'_prod", replace

clear

foreach ind in 10 13 17 20 21 22 23 24 25 26 27 28 29 {
	append using  "$working/Q_klm_opf_`ind'"
}

save "$working/ProdQ_`spec'_opf_byind", replace
